﻿
$(document).ready(function(){
	getOrgInfo();
	getEthicsCommitteeRecordsList();
});

var editStatus = ''; 

function getProvinceList(){
	
	$.ajax({
   		async:false,
		url: '/admin/org!getProvinceList.action',
		type:'POST',
		dataType:'json',
		cache: false,
		success:function(data){
			Interceptor(data);
			var resultContent='';
			$(data).each(function(i) {
				resultContent+='<option value="'+data[i].code+'">'+data[i].name+'</option>';
			});
			$("#province").html(resultContent);
			
			getOrgInfo();
		}
	});
}

function getOrgInfo() {
	$.ajax({
   		async:false,
		url:'/admin/org!findUserOrgById.action',
		type:'POST',
		dataType:'json',
		cache: false,
		success:function(data){
			Interceptor(data);
			var sjreson="";
			var baocunIdStr="";
			
			if(data!=null){
				var dataObj=data.org;
				var status = dataObj.status;
				 
				$("#id").val(dataObj.ID);
				$("#name").val(dataObj.name);
				$("#organType").val(dataObj.organType);
				$("#organLevel").val(dataObj.organLevel);
				$("#organCode").val(dataObj.organCode);
				$("#address").val(dataObj.address);
				$("#postCode").val(dataObj.postCode);
				$("#contactUser").val(dataObj.contactUser);
				$("#contactPhone").val(dataObj.contactPhone);
				$("#contactMobile").val(dataObj.contactMobile);
				$("#contactEmail").val(dataObj.contactEmail);
				$("#province_li").html(dataObj.province.name);
				$("#responsiblePerson").val(dataObj.responsiblePerson);

			}
			
		},
		error:function(){
			//nexts(tag);
		}
	});
}

// 上传文件
KindEditor.ready(function(K) {

	var editor = K.editor({
		uploadJson : '/js/kindeditor-4.1.10/jsp/upload_json.jsp'
		//fileManagerJson : '/assets/plugins/kindeditor-4.1.10/jsp/file_manager_json.jsp',
		//allowFileManager : true
	});
	
	//批准附件（PDF）
	K('#attachmentPath_file').click(function() {
		editor.loadPlugin('image', function() {
			editor.plugin.imageDialog({
				showRemote : false, //隐藏图片浏览
				imageUrl : K('#attachmentPath').val(),
				clickFn : function(url, title, width, height, border, align) {
					$("#attachmentPath").val(url);
					var array = url.split('/');
					$("#attachmentPath_span").html("已上传文件：<a href='" + url + "' target='_blank'>" + array[array.length-1] + "</a>");
					editor.hideDialog();
				}
			});
		});
	});
	
});

function organTypeHandle(value){
  if(value=='其他') {
  	$("#ul_organLevel").hide();
  }else if(value=='医院') {
  	$("#ul_organLevel").show();
  }
}

//zf 2017年2月16日17:18:25 start 
function submitForm() {
	
/*	var organType = $("#organType").val();
	var organLevel = $("#organLevel").val();
	
	if(organType=='医院' && organLevel=='三级甲') {
	}else {
		$("#organLevel").focus();
		alert('提示：干细胞临床研究机构备案仅限于三级甲等医院！');
		return false;
	}
	
	var address = $("#address").val();
	var postCode = $("#postCode").val();
	var contactUser = $("#contactUser").val();
	var contactPhone = $("#contactPhone").val();
	var contactMobile = $("#contactMobile").val();
	var contactEmail = $("#contactEmail").val();
	var responsiblePerson = $("#responsiblePerson").val();
	
	// 备案信息
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	var medicalInstitutionsExequaturPdf = $("#medicalInstitutionsExequaturPdf").val();
	
	$('span[id^="note_"]').html('');
	
	if($.trim(organType)==''){
	  	$("#note_organType").html(' <font style="color:red;">机构类型不可以为空</font>');
	  	$("#organType").focus();
		return false;
	}
	
	if(confirm('您确定要提交吗?')) {
		$.ajax({
	   		async:false,
			url:'/admin/org!saveOrg.action',
			type:'post',
			data:{
				"organType": organType,
				"organLevel": organLevel,
				"address": address,
				"postCode": postCode,
				"contactUser": contactUser,
				"contactPhone": contactPhone,
				"contactMobile": contactMobile,
				"contactEmail": contactEmail,
				"responsiblePerson": responsiblePerson,
				"medicalInstitutionsExequaturPdf": medicalInstitutionsExequaturPdf,
				 
			},
			dataType:'html',
			cache:false,
			success:function(data){
				Interceptor(data);
				if(data!=null && data=="success"){
					alert("提交成功，请等待管理员审核!");
					location.href='/index/organization_records_search.jsp';
				}
			},
			error:function(error){
				//nexts(tag);
			}
		}); 
    	// 	
    }*/
	
	if(confirm('您确定要提交吗?')) {
		$.ajax({
	   		async:false,
			url:'/ethicsCommittee_records/ethics-committee-records!editEthicsCommitteeRecordsSubmit.action',
			type:'post',
			dataType:'html',
			cache:false,
			success:function(data){
				Interceptor(data);
				if(data!=null && data=="success"){
					alert("提交成功，请等待管理员审核!");
					//history.go(-1);
					location.href='/admin/ethicscommittee_records/ethicscommittee_records_input.jsp';
				}
			},
			error:function(error){
				//nexts(tag);
			}
		}); 
    	// 	
    }
	
	
}
//zf 2017年2月16日17:18:25 end

// 编辑证书
function editEthicsCommitteeRecords(id) {
	art.dialog.open('/admin/ethicscommittee_records/win_ethicscommittee_records.jsp?id=' + id, {
   	   title: '伦理委员会备案', 
   	   width: 550, 
   	   height: 420, 
   	   fixed: true,
   	   ok: function() {
   	   	    var iframe = this.iframe.contentWindow;
	    	if (!iframe.document.body) {
	        	alert('iframe还没加载完毕呢');
	        	return false;
	        };
	   		var name = iframe.document.getElementById('name').value;
	   		var memberName = iframe.document.getElementById('memberName').value;
	   		var isConstitution = iframe.document.getElementById('isConstitution').value;
	   		var isWorkingSystem = iframe.document.getElementById('isWorkingSystem').value;
	   		var telphone = iframe.document.getElementById('telphone').value;
	   		var email = iframe.document.getElementById('email').value;
	   		var attachmentPath = iframe.document.getElementById('attachmentPath').value;
	   		var practiceRegistrationAuthority = iframe.document.getElementById('practiceRegistrationAuthority').value;
	   		if(name=='') {
	   			alert("伦理委员会名称不能为空!");
	   			return false;
	   		}else if(memberName=='') {
	   			alert("伦理委员会组成成员名单不能为空！");
	   			return false;
	   		}else if(telphone=='') {
	   			alert("伦理委员会联系电话不能为空!");
	   			return false;
	   		}else if(email=='') {
	   			alert("伦理委员会电子邮箱不能为空!");
	   			return false;
	   		}else if(attachmentPath=='') {
	   			alert("批准附件不能为空!");
	   			return false;
	   		}else if(practiceRegistrationAuthority=='') {
	   			alert("备案执业登记机关不能为空!");
	   			return false;
	   		}else {
	   			$.ajax({
					url : '/ethicsCommittee_records/ethics-committee-records!editEthicsCommitteeRecords.action',
					type : 'post',
					dataType : 'text',
					//使用同步的方式,true为异步方式
					async: false,
					data : {
						'id' : id,
						'name' : name,
						'memberName':memberName,
						'isConstitution':isConstitution,
						'isWorkingSystem':isWorkingSystem,
						'telphone':telphone,
						'email':email,
						'attachmentPath':attachmentPath,
						'practiceRegistrationAuthority':practiceRegistrationAuthority
					},
					success : function(data) {
					 	if(data=='success') {
					 		alert("编辑成功！");
					 		getEthicsCommitteeRecordsList();
					 	}else if(data=='error') {
					 		alert("编辑失败！");
					 		return false;
					 	}
					},
					error : function() {
						// 
					}
				});
	   		}
	   		//
   	   },
   	   cancel: true 
   });
}

// 查询证书列表
function getEthicsCommitteeRecordsList() {
	var status = $("#status").val();
	$.ajax({
		url : '/ethicsCommittee_records/ethics-committee-records!getEthicsCommitteeRecordsList.action',
		type : 'post',
		dataType: 'json',
		success : function(data) {
			 
			 var resultContent = '<tr>'
						      	+'<th style="text-align: left; width: 20%">伦理委员会名称</th>'
						      	+'<th style="text-align: left;">伦理委员会组成成员名单</th>'
						      	+'<th style="text-align: left;">伦理委员会联系电话</th>'
						      	+'<th style="text-align: left;">伦理委员会电子邮箱</th>'
						      	+'<th style="text-align: left;">批准附件</th>'
						      	+'<th style="text-align: left;">状态</th>'
						      	+'<th style="text-align: left;">操作</th>'
						      +'</tr>';
			//zf 2017年2月16日17:18:25 start 
			 var buttonJudge=0;
		     $(data).each(function(i) {
		    	 if(data[i].status == '-1'){
		    		 buttonJudge=1;
		    	 }
		     	 var statusStr = '';
		     	 if(status=='0' || editStatus=='-1') {
		     	 	statusStr = '<a href="javascript:void(0)" onclick="editEthicsCommitteeRecords(' + data[i].id + ')">更新</a>&nbsp; '
				        +	'<a href="javascript:void(0)" onclick="deleteEthicsCommitteeRecords(' + data[i].id + ')">删除</a>';
		     	 }else {
		     	 	statusStr = '无';
		     	 }
		    	 resultContent+='<tr>'
				        +'<td>' + data[i].name + '</td>'
				        +'<td>' + data[i].memberName + '</td>'
				        +'<td>' + data[i].telphone + '</td>'
				        +'<td>' + data[i].email + '</td>'
				        +'<td><a href="' + data[i].attachmentPath + '" target="_blank" >查看/下载</a></td>';
		    	 
		    	 	if(data[i].status == '-1'){
		    	 		resultContent+='<td>草稿</td>';
		    	 	}
		    	 	if(data[i].status == '0'){
		    	 		resultContent+='<td>待审核</td>';
		    	 	}
		    	 	if(data[i].status == '1'){
		    	 		resultContent+='<td>审核通过</td>';
		    	 	}
		    	 	if(data[i].status == '2'){
		    	 		resultContent+='<td>审核未通过</td>';
		    	 	}
		    	 	
		    	 	if(data[i].status == '1'){
		    	 		resultContent+='<td><a href="javascript:void(0)" onclick="editEthicsCommitteeRecords(' + data[i].id + ')">更新</a>&nbsp;</td>';
		    	 	}else{
		    	 		resultContent+='<td>'+ statusStr +'</td>';
		    	 	}  
		    	 		resultContent+='</tr>';
		     });
		     if(data==null || data=='') {
		     	 resultContent+='<tr><td colspan="5" style="text-align:center; height:40px;">数据为空！</td></tr>';
		     }
		     $("#ethics_committee_records_data").html(resultContent);
		     if(buttonJudge == '1'){
		    	 $("#ethics_committee_td_data").html("<input id='tijiaoId'  type='button' value='提交' class='btn' onclick='submitForm();'/>");
		     }
		   //zf 2017年2月16日17:18:25 end
		},
		error : function(X) {
			// 失败
		}
    });
}

// 删除证书
function deleteEthicsCommitteeRecords(id) {
	if(confirm('您确定要删除么?')) {
		$.ajax({
			url : '/ethicsCommittee_records/ethics-committee-records!deleteEthicsCommitteeRecords.action',
			type : 'post',
			data: {
				"id": id
			},
			dataType: 'html',
			success : function(data) {
			     if(data!=null && data=='success') {
			    	 getEthicsCommitteeRecordsList();
			     }
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				// 保存失败
				alert('error:' + textStatus);
			}
	    });
	}
}
